package kim.netty;

import io.netty.channel.Channel;
import io.netty.channel.ChannelId;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;

import java.util.concurrent.ConcurrentHashMap;

public class MyChannelHandlerPool {

    private static final ConcurrentHashMap<String, ChannelId> userChannelBindingMap = new ConcurrentHashMap<>();

    public static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

    public static void put(String userId, ChannelId channelId){
        userChannelBindingMap.put(userId,channelId);
    }

    public static Channel get(String to){
        ChannelId channelId = userChannelBindingMap.get(to);
        if(channelId == null){
            return null;
        }
        return channelGroup.find(channelId);
    }
}
